package cn.cmcc.online.smsapi.ui.view;

import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Color;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import android.widget.TextView;

import org.json.JSONObject;

import java.util.ArrayList;
import java.util.List;

import cn.cmcc.online.smsapi.ui.model.SmsProgress;
import cn.cmcc.online.smsapi.ui.presenter.CarrierOperatorPresenter;
import cn.cmcc.online.smsapi.ui.presenter.CarrierOperatorPresenterImpl;
import cn.cmcc.online.smsapi.ui.util.DensityUtil;
import cn.cmcc.online.smsapi.ui.widget.ExpandButton;
import cn.cmcc.online.smsapi.ui.widget.ProgressTC;
import cn.cmcc.online.smsapi.ui.widget.TextTC;

/**
 * Created by ZhangYifan on 2017/6/19.
 */

public class CarrierOperatorVistaImpl extends GeneralBaseVistaImpl implements CarrierOperatorVista {

    private CarrierOperatorPresenter mPresenter;

    private TextView mEndTime;
    private LinearLayout mMid;
    private ExpandButton mExpandButton;

    public CarrierOperatorVistaImpl(Context context) {
        super(context);
    }

    @Override
    protected void init() {
        super.init();
        mPresenter = new CarrierOperatorPresenterImpl();
        mPresenter.setVista(this);
    }

    @Override
    protected void initLayout() {
        super.initLayout();

        LinearLayout body = new LinearLayout(mContext);
        body.setOrientation(LinearLayout.VERTICAL);
        body.setBackgroundColor(Color.WHITE);
        LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(DensityUtil.dp2px(mContext, 300), -2);
        body.setPadding(0, 0, 0, DensityUtil.dp2px(mContext, 15));
        body.setLayoutParams(layoutParams);
        //----------------------------------------------------------------
        mEndTime = new TextView(mContext);
        mEndTime.setGravity(Gravity.RIGHT);
        mEndTime.setTextColor(Color.parseColor("#999999"));
        mEndTime.setTextSize(12);
        LinearLayout.LayoutParams endTimeParams = new LinearLayout.LayoutParams(-1, -2);
        endTimeParams.setMargins(DensityUtil.dp2px(mContext, 116), DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 15), 0);
        body.addView(mEndTime, endTimeParams);
        //----------------------------------------------------------------
        mMid = new LinearLayout(mContext);
        mMid.setOrientation(LinearLayout.VERTICAL);
        LinearLayout.LayoutParams midParams = new LinearLayout.LayoutParams(-1, -2);
        midParams.setMargins(0, 0, 0, 0);
        body.addView(mMid, midParams);
        //----------------------------------------------------------------
        mExpandButton = new ExpandButton(mContext);
        mExpandButton.setWithAnimation(WITH_ANIMATION);
        mExpandButton.setStateChangedListener(mPresenter);
        LinearLayout.LayoutParams expandButtonParams = new LinearLayout.LayoutParams(-1, -2);
        expandButtonParams.setMargins(DensityUtil.dp2px(mContext, 15), DensityUtil.dp2px(mContext, 7), DensityUtil.dp2px(mContext, 7), 0);
        body.addView(mExpandButton, expandButtonParams);

        super.addBody(body);
    }

    @Override
    public void bind(Context context, JSONObject data) {
        super.bind(context, data);
        mPresenter.bind(data);
    }

    @Override
    public void initDefaultView() {
        super.initDefaultView();
    }

    @Override
    public void setEndTime(String endTime) {
        mEndTime.setText(endTime);
    }

    @Override
    public void setTextList(List<TextTC> textTCList) {
        for (TextTC textTC : textTCList) {
            mMid.addView(textTC);
        }
    }

    @Override
    public TextTC createTextTC() {
        return new TextTC(mContext);
    }

    @Override
    public TextTC updateTextTC(TextTC textTC, String title, String content) {
        textTC.setTitle(title);
        textTC.setContent(content);
        return textTC;
    }

    @Override
    public void setProgressBarList(List<ProgressTC> progressTCList) {
        for (ProgressTC progressTC : progressTCList) {
            mMid.addView(progressTC);
        }
    }

    @Override
    public ProgressTC createProgressTC() {
        return new ProgressTC(mContext);
    }

    @Override
    public ProgressTC updateProgressTC(ProgressTC progressTC, SmsProgress smsProgress) {
        progressTC.setTitle(smsProgress.getTitle());
        progressTC.setProgress(smsProgress);
        return progressTC;
    }

    @Override
    public List<ViewGroup> recycleAllTC() {
        List<ViewGroup> allTC = new ArrayList<>();
        while (mMid.getChildCount() > 0) {
            ViewGroup tc = (ViewGroup) mMid.getChildAt(0);
            allTC.add(tc);
            mMid.removeView(tc);
        }
        return allTC;
    }

    @Override
    public void setEndTimeVisible(int visible) {
        mEndTime.setVisibility(visible);
    }

    @Override
    public void setMidListVisible(int visible) {
        mMid.setVisibility(visible);
    }

    @Override
    public void setExpandButtonColor(int color) {
        mExpandButton.setColor(color);
    }

    @Override
    public void setExpandButtonVisible(int visible) {
        mExpandButton.setVisibility(visible);
    }

    @Override
    public void setTCListHeight(int height, boolean withAnimation) {

        if (withAnimation) {
            ValueAnimator animator = ValueAnimator.ofInt(mMid.getHeight(), height).setDuration(400);
            animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
                @Override
                public void onAnimationUpdate(ValueAnimator animation) {
                    LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mMid.getLayoutParams();
                    params.height = (int) animation.getAnimatedValue();
                    mMid.setLayoutParams(params);
                }
            });
            animator.start();
        } else {
            LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) mMid.getLayoutParams();
            params.height = height;
            mMid.setLayoutParams(params);
        }
    }
}
